package com.lianjinsoft.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 部门：软件开发事业部
 * 功能：
 * 描述：
 * 作成者：李磊
 * 作成时间：2019/8/8
 **/
@Configuration
public class DeadDelayConfig {
    private static final String DELAY_QUEUE_PER_MESSAGE_TTL_NAME = "DELAY_QUEUE_PER_MESSAGE_TTL_NAME";
    private static final String DELAY_EXCHANGE_NAME = "DELAY_EXCHANGE_NAME";
    private static final String DELAY_PROCESS_QUEUE_NAME = "DELAY_PROCESS_QUEUE_NAME";
    private static final String DELAY_QUEUE_PER_QUEUE_TTL_NAME = "DELAY_QUEUE_PER_QUEUE_TTL_NAME";

    @Bean
    Queue delayQueuePerMessageTTL() {
        return QueueBuilder.durable(DELAY_QUEUE_PER_MESSAGE_TTL_NAME)
                .withArgument("x-dead-letter-exchange", DELAY_EXCHANGE_NAME) // DLX，dead letter发送到的exchange
                .withArgument("x-dead-letter-routing-key", DELAY_PROCESS_QUEUE_NAME) // dead letter携带的routing key
                .build();
    }

    @Bean
    Queue delayQueuePerQueueTTL() {
        return QueueBuilder.durable(DELAY_QUEUE_PER_QUEUE_TTL_NAME)
                .withArgument("x-dead-letter-exchange", DELAY_EXCHANGE_NAME) // DLX
                .withArgument("x-dead-letter-routing-key", DELAY_PROCESS_QUEUE_NAME) // dead letter携带的routing key
                .withArgument("x-message-ttl", 10*1000) // 设置队列的过期时间
                .build();
    }

    @Bean
    Queue delayProcessQueue() {
        return QueueBuilder.durable(DELAY_PROCESS_QUEUE_NAME)
                .build();
    }

    @Bean
    DirectExchange delayExchange() {
        return new DirectExchange(DELAY_EXCHANGE_NAME);
    }

    @Bean
    Binding dlxBinding(Queue delayProcessQueue, DirectExchange delayExchange) {
        return BindingBuilder.bind(delayProcessQueue)
                .to(delayExchange)
                .with(DELAY_PROCESS_QUEUE_NAME);
    }

}
